/**
 * Created by M on 2017/9/27.
 */
$(function () {

    if ($("body").height() < $(window).height()) { //设置footer位置
        $("footer.footer").css({"position": "fixed", "bottom": "0", "left": "0"})
    }

    var $websiteTpl = $(".website-tpl"),
        groupId = $("#groupId").val(),
        displayMode = $("#displayMode").val();


    if($websiteTpl.find(".tpl-nav li").length>24){
        $websiteTpl.find(".tpl-nav li:gt(22)").not(".tpl-up").attr("item-hide","hide");
    }

    if($websiteTpl.find(".tpl-nav li:gt(22)").hasClass("on")){
        $websiteTpl.find(".tpl-up").addClass("on");
        $websiteTpl.find(".tpl-nav li[item-hide=hide]").addClass("active");
    }

    $websiteTpl.on("click", ".tpl-up", function () {//展开收起标签
        $(this).toggleClass("on");
        $websiteTpl.find(".tpl-nav li[item-hide=hide]").toggleClass("active");
    });

    $websiteTpl.find(".tpl-layer-code .canvas").each(function () {//生成二维码
        $(this).empty().qrcode({
            render: "canvas",
            width: 110,
            height: 110,
            text: $(this).attr("link")
        });
    });

    $websiteTpl.on("click", ".nav-breads li", function () {//标签筛选
        if ($(this).hasClass("on")||$(this).hasClass("tpl-up"))return;
        $(this).addClass("on").siblings().removeClass("on");
        var tag = $.trim($(this).text());
        location.href = location.pathname + "?tags="+tag+"&groupId="+groupId+"&displayMode=" + displayMode;
    });

    $websiteTpl.on("click", ".tpl-query li", function () {//类型筛选
        if ($(this).hasClass("on"))return;
        $(this).addClass("on").siblings().removeClass("on");
        var url = $(this).data("href");
        location.href = url + "&groupId=" + groupId;
    });

    $websiteTpl.on("click", ".tpl-layer .tpl-use", function () {//点击创建网站
        if (referer)return;

        var itemId=$(this).parents(".tpl-show").find(".item-id b").text();//新用户不需要弹窗创建成功
        $.ajax({
            url: "/get/sitecreate/prompt?templateId="+itemId+"&groupId="+groupId+"&displayMode="+displayMode+"",
            type: "get",
            dataType: "json",
            success: function (json) {
                console.log(json);
                if(!json.login){//未登录
                    location.href=memctx+"/passport/login.html";
                    return;
                }
                if(json.showPrompt){//有弹窗
                    var $html=$($("#tpl-template").html());
                    $html.find(".hint-purchase strong").text(json.message);
                    if (!json.canCreate) {
                        $html.find(".hint-purchase a").addClass("hint-close");
                    }
                    if(json.canCreate){//可以创建
                        $html.find("a").attr("href",pcctx+"/setting/newsite/"+itemId+".html?groupId="+groupId+"&displayMode="+displayMode+"");
                    }
                    $html.appendTo($("body"));
                    return;
                }

                location.href=pcctx+"/setting/newsite/"+itemId+".html?groupId="+groupId+"&displayMode="+displayMode+"";

            }
        });


    });


    $(document).on("click", ".page-pop-outer .close,.page-pop-outer .hint-close", function () {
        $(".page-pop-outer").remove();
    });



    $websiteTpl.on("click", ".tpl-query i", function () {//input
        var text = $.trim($(this).prev().val());
        if (text == "")return;
        var tags=$(this).next().val();
        location.href = "/theme/searchkey.html?keyword="+text+"&groupId="+groupId+"&tags="+tags+"&displayMode="+displayMode+"";
    });

    $websiteTpl.on("keydown", "input[name=keyword]", function (e) {//input
        if(e.keyCode==13)$(this).next("i").trigger("click");
    });

    $(document).on("mouseenter", ".handel-template .code", function () {
        $(this).parents(".tpl-show").find(".tpl-layer-code").addClass("on");

    }).on("mouseleave", ".handel-template .code", function () {
        $(this).parents(".tpl-show").find(".tpl-layer-code").removeClass("on");
    });


    $(document).on("mouseenter", ".handel-template .attr", function () {
        $(this).parents(".tpl-show").find(".tpl-layer-attr").addClass("on");

    }).on("mouseleave", ".handel-template .attr", function () {
        $(this).parents(".tpl-show").find(".tpl-layer-attr").removeClass("on");
    });

    $(window).load(function () {//中文转换
        var $urlTagsVal = $.getUrlParam('tags');
        encodeURI($urlTagsVal);
    });

});
$.getUrlParam = function (name) {//获取网址参数
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return (decodeURIComponent(r[2]));
    return null;
};
